package com.bdqn.struts.dao.impl;

import com.bdqn.struts.dao.LoginDao;
import com.bdqn.struts.entity.Login;
import com.bdqn.struts.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;


public class LoginDaoImpl extends HibernateDaoSupport implements LoginDao {
     Transaction tx=null;
    Session session=null;


    @Override
    public Login getLogin(String userName, String passWord) {
            session= HibernateUtil.getSession();
            tx=session.beginTransaction();
            String hql="from login where userName=? and passWord=?";
            Query query=session.createQuery(hql);
            query.setParameter(0,userName);
            query.setParameter(1,passWord);
            Login login=(Login) query.uniqueResult();
            return login;
        }

    @Override
    public List<Login> getAllLogin() {
        session = HibernateUtil.getSession();
        tx = session.beginTransaction();
        String hql = "from Login";
        Query query = session.createQuery(hql);
        List<Login> loginList = query.list();
        return loginList;
    }

    @Override
    public Login getLoginByTem(String username, String Password) {
        Object[] param={username,Password};
        String hql="select * from login where username=? and password=?";
        return (Login) this.getHibernateTemplate().find(hql,param);
    }

    @Override
    public void save(Login login) {
        this.getHibernateTemplate().save(login);
    }
}
